<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {assign} compiler function plugin
 *
 * Type:     compiler function<br>
 * Name:     assign<br>
 * Purpose:  assign a value to a template variable
 * @link http://smarty.php.net/manual/en/language.custom.functions.php#LANGUAGE.FUNCTION.ASSIGN {assign}
 *       (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com> (initial author)
 * @auther messju mohr <messju at lammfellpuschen dot de> (conversion to compiler function)
 * @param string containing var-attribute and value-attribute
 * @param Smarty_Compiler
 */
function smarty_compiler_column($tag_attrs, &$smarty)
{
	$_params = preg_split ("/[\s,]+/", $tag_attrs);

	foreach($_params as $val){
		list($k,$v) = explode('=',$val);
		$params[$k] = trim($v,"'");
		$params[$k] = trim($params[$k],'"');
	}

	$columns = $smarty->_tpl_vars['columns'];

	if (isset($params['cid']))
	{
		$column = $columns[$params['cid']];
	} 
	else if (isset($params['cw']))
	{
		$word = $params['cw'];
		$percent = array();
		foreach ($columns as $val)
		{
			similar_text($word,$val['classname'],$per);
			$percent[$val['classid']] = $per;
		}
		natsort($percent);
		$c = array_keys($percent);
		$num = count($c);
		$cl = $column = $columns[$c[($num-1)]];
	}

	if (isset($column[$params['field']])){
		$smarty->assign('column_field',$column[$params['field']]);
		return "require_once(SMARTY_CORE_DIR . '../plugins/compiler.column.php');smarty_compiler_column(\"$tag_attrs\", &\$this);echo \$this->_tpl_vars['column_field']";
	} else {
		return "smarty_compiler_column(\"$tag_attrs\", &\$this);";
	}
}
?>